<?php 

class Dictionary {
	
	private $db = null;
	private $rq;
	private $fname = '';
	
	public function __construct($name,$db=null){
		$this->rq = Zend_Controller_Front::getInstance()->getRequest();
		$this->fname = 'D_'.$name;
		$this->db = $db;
	}
	
	public function go(){
		$fname = $this->fname;
		if(method_exists($this, $fname)){
			return $this->$fname();
		} else {
			Validation::react('Niepoprawny słownik.');
		}
	}
	
	/**
	 * SŁOWNIKI
	 */
	private function D_bible(){
		return $this->processor('bible','bible_id','bible_name');
	}
	
	private function D_movie(){
		return $this->processor('movie','movie_id','movie_title');
	}
	
	private function D_language(){
		return $this->processor('language','language_id','language_name');
	}
	
	private function D_tag(){
		return $this->processor('tag','tag_id','tag_name');
	}
	
	private function D_book(){
		return $this->processor('book','book_id','book_workname');
	}
	
	private function D_testament(){
		return $this->processor('testament','testament_id','testament_name');
	}
	
	private function D_chapter(){
		return $this->processor('chapter','chapter_id','chapter_number');
	}
	
	private function D_biblebook(){
		return $this->processor('bible_book','bible_book_id','bible_book_name');
	}
	
	private function D_bibleverse(){
		return $this->processor('bible_verse','bible_verse_id','bible_verse_text');
	}
	
	private function D_verse(){
		return $this->processor('verse','verse_id','CONCAT(book_workname," - ",chapter_number," - ",verse_number)',array('verse_number'),array('joins'=>array(
			array(array('b'=>'book'),'b.book_id = d.book_id',array('book_workname')),
			array(array('c'=>'chapter'),'c.chapter_id = d.chapter_id',array('chapter_number'))
		)),null,array('chapter_number ASC','verse_number ASC'));
	}
	
	private function D_author(){
		return $this->processor('author','author_id','CONCAT(author_firstname," ",author_middlename," ",author_lastname)');
	}
	
	private function D_user(){
		return $this->processor('user','user_id','CONCAT(user_firstname," ",user_lastname)');
	}
	
	private function D_denomination(){
		return $this->processor('denomination','denomination_id','denomination_name');
	}
	
	private function D_aclrole(){
		return $this->processor('acl_role','acl_role_id','acl_role_name');
	}
	
	private function D_argument(){
		return $this->processor('dilema_point','dilema_point_id','dilema_point_head');
	}
	
	
	/**
	 * HELPER
	 * 
	 * @param $table
	 * @param $idField
	 * @param $nameField
	 * @param $addFields
	 */
	private function processor($table,$idField,$nameField,$addFields=array(),$join=null,$where=null,$order=null){
		
		$rq = $this->rq;
    	$db = !$this->db ? Zend_Db_Table::getDefaultAdapter() : $this->db;
    	$s = $db->select();
    	
    	$q = $rq->getParam('query');
		
    	$fields = array($idField.' AS id',$nameField.' AS name');// samo się quotuje!
    	    	
    	for ($i=0;$i<count($addFields);$i++){
    		array_push($fields,$addFields[$i]);//brakuje quotowania
    	}
    	
    	$rqwhere = Utils::createWhereStatement($rq->getParam('where'));

    	if($join){
    		if(array_key_exists('joins', $join)){
    			foreach($join['joins'] as $j){
    				
    				$s->join($j[0],$j[1],$j[2]);	
    			}
    		} else {
    			$s->join($join[0],$join[1],$join[2]);
    		}
    	}
    	
    	$s->from(array('d'=>$table),$fields);
    	
    	if($q)$s->where($nameField.' like "'.$q.'%"');
		if($rqwhere)$s->where($rqwhere);
		if($where)$s->where($where);
		
		if(strpos($idField, 'CONCAT')===false){
			$s->group('d.'.$idField);
		} else {
			$s->group('id');
		}
		
		$s->order($order ? $order : $nameField.' ASC');
		//$s->order($nameField.' ASC');
		
		//return Validation::sayOk($s->assemble());
		
		$r = $db->query($s)->fetchAll();
		
		if(is_string($rq->getParam('all'))){
			array_unshift($r,array("id"=>'',"name"=>$rq->getParam('all'),'active'=>1));
		}
		
		return json_encode(array("success"=>true,"rows"=>$r));
				
	}
	
}

?>